package com.example.elderlycare.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.elderlycare.controller.entity.EquipModelRequestBody;
import com.example.elderlycare.dao.entity.EquipModel;
import com.example.elderlycare.service.EquipModelService;
import com.example.elderlycare.service.entity.EquipModelListResponseBody;
import com.example.elderlycare.utils.Result;
import com.example.elderlycare.utils.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;


@RestController
@RequestMapping("/equipModel")
public class EquipModelController {
    private final Logger logger = LoggerFactory.getLogger(EquipmentController.class);

    @Resource
    EquipModelService equipModelService;

    /**
     * queryEquipModelList 根据条件查询列表
     *
     * @param type     设备类型
     * @param model    型号名称
     * @param supplier 厂商
     * @param pageSize 页面容量
     * @param current  当前页码
     * @return EquipModelListResponseBody
     */
    @GetMapping
    public Result<EquipModelListResponseBody> queryEquipModelList(
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "model", required = false) String model,
            @RequestParam(value = "supplier", required = false) String supplier,
            @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
            @RequestParam(defaultValue = "1", value = "current") int current
    ) {
        QueryWrapper<EquipModel> queryWrapper = new QueryWrapper<>();
        if (type != null && !"".equals(type)) {
            queryWrapper.like("equip_type", type);
        }
        if (model != null && !"".equals(model)) {
            queryWrapper.like("model_name", model);
        }
        if (supplier != null && !"".equals(supplier)) {
            queryWrapper.like("supplier", supplier);
        }
        EquipModelListResponseBody responseBody = equipModelService.queryEquipModelList(current, pageSize, queryWrapper);
        if (responseBody == null) {
            Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(responseBody);
    }

    /**
     * addEquipModel 增加型号
     *
     * @param req EquipModelRequestBody.AddEquipModelRequestBody
     * @return null
     */
    @PostMapping
    public Result<?> addEquipModel(@Valid @RequestBody EquipModelRequestBody.AddEquipModelRequestBody req) {
        EquipModel equipModel = EquipModel.builder().
                modelName(req.getModelName()).
                equipType(req.getEquipType()).
                supplier(req.getSupplier()).build();
        boolean success = equipModelService.save(equipModel);
        if (success) {
            return Result.success(null);
        } else {
            logger.warn("add equipModel error");
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }


    /**
     * updateEquipModel 根据id更改型号信息
     *
     * @param req EquipModelRequestBody.UpdateEquipModelRequestBody
     * @return null
     */
    @PutMapping
    public Result<?> updateEquipModel(@Valid @RequestBody EquipModelRequestBody.UpdateEquipModelRequestBody req) {
        EquipModel equipModel = EquipModel.builder().
                id(req.getId()).
                modelName(req.getModelName()).
                equipType(req.getEquipType()).
                supplier(req.getSupplier()).build();
        boolean success = equipModelService.updateById(equipModel);
        if (success) {
            return Result.success(null);
        } else {
            logger.warn("update equipModel error");
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }


    /**
     * deletEquipModel 根据id删除
     *
     * @param id 需要删除信息的id
     * @return null
     */
    @DeleteMapping
    public Result<?> deletEquipModel(@RequestParam("id") @NotEmpty Integer id) {
        boolean success = equipModelService.removeById(id);
        if (success) {
            return Result.success(null);
        } else {
            logger.warn("delete equipModel error");
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }
}
